package reaction.news.index;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NewsArticle {
	
	public int id;
	public int category;
	public String title;
	public String subtitle;
	public String text;
	
	public Map<String,String> pessoas;
	public Map<String,String> organizacoes;
	public Map<String,String> locais;
	
	public org.w3c.dom.Document xml;

	
	
	public NewsArticle(int id_, int category_, String title_, String subtitle_, String text_) {
		this.id = id_;
		this.category = category_;
		this.title = title_;
		this.subtitle = subtitle_;
		this.text = text_;
	}
	
	
	public NewsArticle() {
		super();
		pessoas = new HashMap<String,String>();
		locais = new HashMap<String,String>();
		organizacoes = new HashMap<String,String>();
	}
	
	
	public NewsArticle(int id_, int category_id_) {
		this.id = id_;
		this.category = category_id_;
		pessoas = new HashMap<String,String>();
		locais = new HashMap<String,String>();
		organizacoes = new HashMap<String,String>();
	}
	
	
	public void extractNE(Map<String,String> entities, String NEType) {
		
		NodeList nodes = xml.getElementsByTagName(NEType);
		
	    for (int i = 0; i < nodes.getLength(); i++) {
			Node node = nodes.item(i);
			entities.put(node.getTextContent().trim(), "");
		}
	}
	
	public Document luceneDocument() {
		
		Document doc = new Document();
		doc.add(new Field("id", Integer.toString(this.id), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
		doc.add(new Field("category", Integer.toString(this.category), Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.NO));
		doc.add(new Field("title", this.title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
		doc.add(new Field("subtitle", this.subtitle, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
		doc.add(new Field("text", this.text, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
		
		return doc;
	}
	
}
